<?php
namespace common\lib;

use Yii;

class Page {
    private $pageSize;
    private $where;
    private $orderby;
    private $model;
    private $total;
    private $currentPage;
    private $maxPage;
    private $fields;
    private $format;//1：默认格式（全），2：简洁格式，页面列表格式


    public function __construct() {
        $this->setPageSize();
        $this->setFormat();
    }
    
    public function setPageSize($pageSize = null){
        if(empty($pageSize)){
            $pageSize = 10;
        }
        $this->pageSize = $pageSize;
        return $this;
    }
    
    public function setWhere($where = ''){
        $this->where = $where;
        return $this;
    }
    
    public function setOrderBy($orderby){
        $this->orderby = $orderby;
        return $this;
    }
    
    public function setModel($model){
        $this->model = $model;
        return $this;
    }
    
    public function setCurrentPage($page){
        $this->currentPage = $page;
        return $this;
    }
    
    public function setFormat($format = null){
        if(empty($format)){
            $format = 1;
        }
        $this->format = $format;
        return $this;
    }
    
    public function setFields($fields){
        $this->fields = $fields;
        return $this;
    }

    public function getPage(){
        //这里是必须按照顺序进行的
        $this->getTotal();
        $this->getMaxPage();
        $this->getCurrentPage();
        $start = ($this->currentPage - 1) * $this->pageSize;
        $m = $this->model->select('*');
        if(!empty($this->where)){
            $m->where($this->where);
        }
        if(!empty($this->orderby)){
            $m->orderby($this->orderby);
        }
        if(!empty($this->fields)){
            $m->select($this->fields);
        }
        $data = $m->offset($start)->limit($this->pageSize)->asArray()->all();
        $pageCount = count($data);
        $pageStr = $this->getPageStr($pageCount);
        return ['data' => $data, 'pageStr' => $pageStr];
    }
    
    /**
     * 取得分类信息
     */
    private function getPageStr($pageCount){
        switch($this->format){
            case 1: //默认格式
                return $this->getDefaultPageStr($pageCount);
                break;
            case 2: //简洁格式
                return $this->getSimplePageStr();
                break;
            case 3: //页码格式
                return $this->getListPageStr();
                break;
            case 4: //api格式
                return $this->getApiPageData($pageCount);
                break;
            default:
                return $this->getDefaultPageStr($pageCount);
                break;
        }
    }
    
    public function getApiPageData($pageCount){
        return [
            'total' => $this->total,
            'maxPage' => $this->maxPage,
            'currentPage' => $this->currentPage,
            'currentCount' => $pageCount,
            'pageSize' => $this->pageSize,
        ];
    }


    private function getDefaultPageStr($pageCount){
        $str = '<div class="pager" currentPage="'.$this->currentPage.'" maxPage="'.$this->maxPage.'">
                <span>共'.$this->maxPage.'页</span>
                <span>当前第'.$this->currentPage.'页</span>
            ';
        if($this->maxPage <= 1){
            $str .= '
                <span>第一页</span>
                <span>上一页</span>
                <span>下一页</span>
                <span>最末页</span>
            ';
        }elseif($this->maxPage > 1){
            if($this->currentPage == 1){
                $str .= '
                    <span>第一页</span>
                    <span>上一页</span>
                    <a href="javascript:void(0);" class="nextPage">下一页</a>
                    <a href="javascript:void(0);" class="lastPage">最末页</a>
                ';
            }elseif($this->currentPage > 1 && $this->currentPage < $this->maxPage){
                $str .= '
                    <a href="javascript:void(0);" class="firstPage">第一页</a>
                    <a href="javascript:void(0);" class="priorPage">上一页</a>
                    <a href="javascript:void(0);" class="nextPage">下一页</a>
                    <a href="javascript:void(0);" class="lastPage">最末页</a>
                ';
            }elseif($this->currentPage > 1 && $this->currentPage == $this->maxPage){
                $str .= '
                    <a href="javascript:void(0);" class="firstPage">第一页</a>
                    <a href="javascript:void(0);" class="priorPage">上一页</a>
                    <span>下一页</span>
                    <span>最末页</span>
                ';
            }elseif($this->currentPage > $this->maxPage){
                $str .= '
                    <a href="javascript:void(0);" class="firstPage">第一页</a>
                    <a href="javascript:void(0);" class="priorPage">上一页</a>
                    <span>下一页</span>
                    <span>最末页</span>
                ';
            }
        }
        $str .= '<span><input type="text" size="2"><button class="setPage">go</button></span>';
        return $str . '</div>';
    }

    /**
     * 取得总数
     */
    private function getTotal(){
        $m = clone $this->model;
        $m->select('COUNT(*) AS total');
        $data = !empty($this->where) ? $m->where($this->where)->asArray()->one() : $m->asArray()->one();
        $this->total = $data['total'];
    }
    
    /**
     * 渠道最大页数
     */
    private function getMaxPage(){
        $this->maxPage = ceil($this->total / $this->pageSize);
        if($this->maxPage <= 0){
            $this->maxPage = 1;
        }
    }
    
    /**
     * 取得当前页数
     */
    private function getCurrentPage(){
        $p = $this->currentPage;
        if($p > $this->maxPage){$p = $this->maxPage;}
        if($p < 1){$p = 1;}
        $this->currentPage = $p;
    }
    
}
